﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using System.Windows.Controls;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class ArrivalRegisterModify : EFViewCmes
    {
        public string InsertMeg = "";
        public ArrivalRegisterModify(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }
        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    this.fdMaterialName.IsReadOnly = true;
                    this.fdMetric.IsEnabled = false;
                    this.fdSpecModel.IsReadOnly = true;
                    this.fdNumber.IsReadOnly = true;
                    this.fdQuality.IsReadOnly = true;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    this.fdMaterialCode.eLabel = "物资编码";
                    this.fdMaterialCode.IsEnabled = false;
                    this.fdMaterialName.IsReadOnly = true;
                    this.fdMetric.IsEnabled = false;
                    this.fdSpecModel.IsReadOnly = true;
                    this.fdNumber.IsReadOnly = true;
                    this.fdQuality.IsReadOnly = true;
                    this.fdPlanNum.IsReadOnly = true;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }
        
        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            if (this.fdIsfromPlan.Value.Equals("1"))
            {
                double fdActualNum = 0;
                double fdPlanNum = 0;
                double fdNotReceiveNum = 0;
                if(!this.fdActualNum.Value.Equals(""))
                    Double.TryParse(this.fdActualNum.Value.ToString(), out fdActualNum);
                if(!this.fdPlanNum.Value.Equals(""))
                    Double.TryParse(this.fdPlanNum.Value.ToString(), out fdPlanNum);
                if (!this.fdNotReceiveNum.Value.Equals(""))
                    Double.TryParse(this.fdNotReceiveNum.Value.ToString(), out fdNotReceiveNum);

                if (fdActualNum != 0 && fdNotReceiveNum < fdActualNum)
                {
                    base.OpenEFWindowAlert("新增的物资明细来自采购计划，到货数量不允许大于未到货数量！", true);
                    return;
                }

                if (this.fdMaterialCode.IsEnabled && fdPlanNum != 0 && fdNotReceiveNum < fdPlanNum)
                {
                    base.OpenEFWindowAlert("新增的物资明细来自采购计划，应收数量不允许大于未到货数量！", true);
                    return;
                }

                if (fdPlanNum < fdActualNum)
                {
                    base.OpenEFWindowAlert("到货数量不允许大于应收数量！", true);
                    return;
                }
            }

            base.OnCommitButtonClick("ZMCMF1", sender, e);
        }

        /// <summary>
        /// 点击"确定"后的回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void DoCommitCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                InsertMeg = inInfo.GetDetailMsg();//新增后的回调方法，用于提醒用户，如需调整计划价，请进入调价模块
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 物资编码改变，变更其它值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdMaterialCode_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;
            if (picker.ItemObject != null && !"".Equals(picker.ItemObject))
            {
                if (this.fdIsfromPlan.Value.Equals("0"))
                {
                    this.fdMaterialId.Value = picker.ItemObject.GetType().GetProperty("fdKeyId").GetValue(picker.ItemObject, null);
                }
                else
                {
                    this.fdMaterialId.Value = picker.ItemObject.GetType().GetProperty("fdMaterialId").GetValue(picker.ItemObject, null);
                }
                this.fdMaterialName.Value = picker.ItemObject.GetType().GetProperty("fdMaterialName").GetValue(picker.ItemObject, null);
                this.fdQuality.Value = picker.ItemObject.GetType().GetProperty("fdQuality").GetValue(picker.ItemObject, null);
                this.fdSpecModel.Value = picker.ItemObject.GetType().GetProperty("fdSpecModel").GetValue(picker.ItemObject, null);
                this.fdNumber.Value = picker.ItemObject.GetType().GetProperty("fdNumber").GetValue(picker.ItemObject, null);
                this.fdPrice.Value = picker.ItemObject.GetType().GetProperty("fdPrice").GetValue(picker.ItemObject, null);
                this.fdMetric.Value = picker.ItemObject.GetType().GetProperty("fdMetric").GetValue(picker.ItemObject, null);

                if (this.fdIsfromPlan.Value.Equals("1"))
                {
                    this.fdPlanNum.Value = picker.ItemObject.GetType().GetProperty("fdPlanNum").GetValue(picker.ItemObject, null);
                    this.fdNotReceiveNum.Value = picker.ItemObject.GetType().GetProperty("fdPlanNum").GetValue(picker.ItemObject, null);
                    this.fdStockPlanId.Value = picker.ItemObject.GetType().GetProperty("fdStockPlanId").GetValue(picker.ItemObject, null);
                }
                this.fdStockPlanDetailId.Value = picker.ItemObject.GetType().GetProperty("fdMaterialCode").GetValue(picker.ItemObject, null);
            }
            else
            {
                this.fdMaterialId.Value = "";
                this.fdMaterialName.Value = "";
                this.fdQuality.Value = "";
                this.fdSpecModel.Value = "";
                this.fdNumber.Value = "";
                this.fdPrice.Value = "";
                this.fdMetric.Value = "";
                this.fdNotReceiveNum.Value = "";
                this.fdStockPlanId.Value = "";
                this.fdStockPlanDetailId.Value = "";
            }
        }

        /// <summary>
        /// 计算到货金额
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdActualNum_LostFocus(object sender, RoutedEventArgs e)
        {
            var fdPrice = this.fdPrice.Value;
            var fdActualNum = this.fdActualNum.Value;

            double jg = 0;
            if (fdPrice != null)
            {
                if (!fdPrice.ToString().Equals(""))
                    Double.TryParse(this.fdPrice.Value.ToString(), out jg);
            }
            double sqsl = 0;
            if (fdActualNum != null)
            {
                if (!fdActualNum.ToString().Equals(""))
                    Double.TryParse(this.fdActualNum.Value.ToString(), out sqsl);
            }
            double je = jg * sqsl;
            this.fdReceiveSum.Value = String.Format("{0:0.#####}", je);
        }

        /// <summary>
        /// 计算实际价格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdReceiveSum_LostFocus(object sender, RoutedEventArgs e)
        {
            var fdActualNum = this.fdActualNum.Value;
            var fdReceiveSum = this.fdReceiveSum.Value;

            double je = 0;
            if (fdReceiveSum != null)
            {
                if (!fdReceiveSum.ToString().Equals(""))
                    Double.TryParse(fdReceiveSum.ToString(), out je);
            }
            double sjsl = 0;
            if (fdActualNum != null)
            {
                if (!fdActualNum.ToString().Equals(""))
                    Double.TryParse(fdActualNum.ToString(), out sjsl);
            }
            double jg = je / sjsl;
            this.fdPrice.Value = String.Format("{0:0.#####}", jg);
        }
    }
}
